package com.hospitalsystem.controller;

import com.hospitalsystem.entity.Doctor;
import com.hospitalsystem.service.ArrangeService;
import com.hospitalsystem.service.DoctorService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Doctor)表控制层
 *
 * @author makejava
 * @since 2024-12-28 19:32:19
 */
@RestController
@RequestMapping("doctor")
public class DoctorController {
    /**
     * 服务对象
     */
    @Resource
    private DoctorService doctorService;

    @Resource
    private ArrangeService arrangeService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/getAllDoctor")
    public ResponseEntity<Page<Doctor>> queryByPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return ResponseEntity.ok(this.doctorService.queryByPage(pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param dId
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Doctor> queryById(@PathVariable("id")String dId) {
        return ResponseEntity.ok(this.doctorService.queryById(dId));
    }

    /**
     * 新增数据
     *
     * @param doctor 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Doctor> add(Doctor doctor) {
        return ResponseEntity.ok(this.doctorService.insert(doctor));
    }

    /**
     * 编辑数据
     *
     * @param doctor 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Doctor> edit(Doctor doctor) {
        return ResponseEntity.ok(this.doctorService.update(doctor));
    }

    /**
     * 删除数据
     *
     * @param dId
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String dId) {
        return ResponseEntity.ok(this.doctorService.deleteById(dId));
    }


    /**
     * 医生登录
     *
     * @param dId
     * @param password
     * @return 登录是否成功，type
     */
    @GetMapping("/login/{dId}/{password}")
    public ResponseEntity<Map<String, Object>> login(@PathVariable("dId") String dId,@PathVariable("password") String password){
//        return ResponseEntity.ok(this.doctorService.login(dId,password));

        String type = this.doctorService.login(dId, password);
        String name = this.doctorService.getNameByDId(dId);
        // 构造一个响应 Map
        Map<String, Object> response = new HashMap<>();
        response.put("success", this.doctorService.existIdAndPassword(dId,password));
        response.put("type", type); // 包含原始的 type 信息
        response.put("name",name);

        return ResponseEntity.ok(response);
    }

    /**
     * 账号和密码对应的医生是否存在
     *
     * @param dId
     * @param password
     * @return 是否存在
     */
    @GetMapping("exist/{dId}/{password}")
    public ResponseEntity<Map<String, Boolean>> existIdAndPassword(@PathVariable("dId") String dId, @PathVariable("password") String password){
        boolean exists = this.doctorService.existIdAndPassword(dId,password);
        Map<String, Boolean> response = new HashMap<>();
        response.put("exist",exists);
        return ResponseEntity.ok(response);
    }


    /**
     * 根据前端传入的时刻查询对应时间段内的医生，并返回每个医生的剩余名额
     *
     * @param arrangeTime
     * @return 是否存在
     */
    @GetMapping("/getDoctorsWithAvailableSlots")
    public ResponseEntity<List<Doctor>> getDoctorsWithAvailableSlots(
            @RequestParam(value = "arrangeTime", required = false) String arrangeTime) {

        // 如果前端没有传入时刻，则查询所有医生并返回
        if (arrangeTime == null || arrangeTime.isEmpty()) {
            System.out.println("arrangeTime为空");
            List<Doctor> doctors = doctorService.getAllDoctors(); // 查询所有医生，不包括剩余名额
            return ResponseEntity.ok(doctors);
        }

        // 否则，查询该时刻所在一小时内有剩余名额的所有医生，并返回剩余名额
        List<Doctor> availableDoctors = doctorService.getDoctorsWithAvailableSlots(arrangeTime);
        return ResponseEntity.ok(availableDoctors);
    }

    /**
     * 根据前端传入的时刻和科室查询对应时间段内的医生，并返回每个医生的剩余名额
     *
     * @param arrangeTime
     * @param department
     * @return 是否存在
     */
    @GetMapping("/getDoctorsWithAvailableSlots/ByDepartment")
    public ResponseEntity<List<Doctor>> getDoctorsWithAvailableSlotsByDepartment(
            @RequestParam(value = "arrangeTime", required = false) String arrangeTime,@RequestParam(value = "department") String department) {
        // 查询该时刻所在一小时内有剩余名额的所有医生，并返回剩余名额
        List<Doctor> availableDoctors = doctorService.getDoctorsWithAvailableSlotsByDepartment(arrangeTime,department);
        return ResponseEntity.ok(availableDoctors);
    }

    /**
     * 获取某一科室所有医生
     *
     * @param department
     * @return 是否存在
     */
    @GetMapping("/getDoctorsByDepartment")
    public ResponseEntity<List<Doctor>> getDoctorsByDepartment(
            @RequestParam(value = "department") String department) {
        List<Doctor> doctors = doctorService.getDoctorsByDepartment(department);
        return ResponseEntity.ok(doctors);
    }

    /**
     * 叫号
     *
     * @param dId
     * @param pId
     * @return 更新if_finish是否成功
     */
    @GetMapping("/callPatient")
    public ResponseEntity<Boolean> callPatient(@RequestParam(value = "dId") String dId, @RequestParam(value = "pId") String pId){
        return ResponseEntity.ok(arrangeService.callPatient(dId, pId));
    }

    /**
     * 根据mId修改if_take
     *
     * @param mId
     * @return 更新if_take是否成功
     */
    @GetMapping("/updateIfTake1")
    public ResponseEntity<Boolean> updateIfTake1(@RequestParam(value = "mId") String mId){
        return ResponseEntity.ok(doctorService.updateIfTake1(mId));
    }

    /**
     * 根据mId修改if_take
     *
     * @param mId
     * @return 更新if_take是否成功
     */
    @GetMapping("/updateIfTake_1")
    public ResponseEntity<Boolean> updateIfTake_1(@RequestParam(value = "mId") String mId){
        return ResponseEntity.ok(doctorService.updateIfTake_1(mId));
    }

    /**
     * 根据mId修改if_pay
     *
     * @param mId
     * @return 更新if_pay是否成功
     */
    @GetMapping("/updateIfPay")
    public ResponseEntity<Boolean> updateIfPay(@RequestParam(value = "mId") String mId){
        return ResponseEntity.ok(doctorService.updateIfPay(mId));
    }

}

